package com.neuedu.thread;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/3114:27
 */
public class TimerThread  extends  Thread {

    private int  time;

    public TimerThread(String name, int time) {
        super(name);
        this.time = time;
    }

    @Override
    public void run() {

        int i= 0;
        while ( i++ < time ){

            System.out.println(getName()+":在执行");
            try {
                // yield();
                Thread.yield();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static void main(String[] args) {

        TimerThread t1 = new TimerThread("张三吃饭", 6);
        TimerThread t2 = new TimerThread("李四睡觉", 999999);


        t1.setPriority(2);
        t1.start();
        t2.start();

        while (true){


            System.out.println("t1.isAlive() = " + t1.isAlive() +"getPriority ："+t1.getPriority());
            System.out.println("t2.isAlive() = " + t2.isAlive());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }



    }
}
